import json
import logging
from abc import ABC, abstractmethod
from typing import Dict, Any

# 初始化日志器
logger = logging.getLogger(__name__)


class BaseProcessor(ABC):
    """处理器基类"""

    def __init__(self, volcano_client):
        self.volcano_client = volcano_client

    @abstractmethod
    async def process(self, inputs: Dict[str, Any]):
        """处理输入并返回结果"""
        pass

    async def call_model(self, system_prompt: str, user_prompt: str) -> str:
        """调用火山大模型"""
        try:
            result = await self.volcano_client.call_volcano_api(
                system_prompt, user_prompt
            )
            return result
        except Exception as e:
            logger.error(f"调用大模型失败: {str(e)}")
            return json.dumps({"error": f"调用大模型失败: {str(e)}"})

    def parse_json_response(self, response: str) -> Any:
        """解析大模型返回的JSON响应"""
        try:
            # 尝试直接解析为JSON
            return json.loads(response)
        except json.JSONDecodeError:
            logger.warning(f"JSON解析失败，返回原始响应: {response}")
            return response